package com.bw.shoppingmall;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.goods.mvvm.view.fragment.CategoryFragment;
import com.bw.goods.mvvm.view.fragment.HomeGoodsFragment;
import com.bw.msg.mvvm.view.fragment.MessageFragment;
import com.bw.mvvm_comom.utils.SpUtils;
import com.bw.shopping.mvvm.view.CarFragment;
import com.bw.shoppingmall.wiget.BottomButton;
import com.bw.user.mvvm.view.fragment.UserFragment;

public class MainActivity extends AppCompatActivity implements BottomButton.BottomSelectLisenner {

    private BottomButton index;
    private BottomButton sort;
    private BottomButton buyCar;
    private BottomButton news;
    private BottomButton mine;
    private Fragment goodsFragment,categoryFragment,carFragment,messageFragment,userFragment;
    FragmentTransaction fragTransaction;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this,true);

        LogUtils.e(SpUtils.readToken().toString());

        index = (BottomButton) findViewById(R.id.index);
        sort = (BottomButton) findViewById(R.id.sort);
        buyCar = (BottomButton) findViewById(R.id.buyCar);
        news = (BottomButton) findViewById(R.id.news);
        mine = (BottomButton) findViewById(R.id.mine);

        index.setContent("首页")
                .setnomalColor(Color.BLACK)
                .setselColor(Color.parseColor("#03A9F4"))
                .setNomalImg(R.drawable.indexfalse)
                .setSelImg(R.drawable.indextrue)
                .setNomalsize(5)
                .setSelFlag(true)
                .setselSize(7)
                .setLinearlisenner(this)
                .refresh();
        sort.setContent("分类")
                .setnomalColor(Color.BLACK)
                .setselColor(Color.parseColor("#03A9F4"))
                .setNomalImg(R.drawable.categoryfalse)
                .setSelImg(R.drawable.categorytrue)
                .setNomalsize(5)
                .setselSize(7)
                .setLinearlisenner(this)
                .refresh();
        buyCar.setContent("购物车")
                .setnomalColor(Color.BLACK)
                .setselColor(Color.parseColor("#03A9F4"))
                .setNomalImg(R.drawable.buycarfalse)
                .setSelImg(R.drawable.buycar)
                .setNomalsize(5)
                .setselSize(7)
                .setLinearlisenner(this)
                .refresh();
        news.setContent("消息")
                .setnomalColor(Color.BLACK)
                .setselColor(Color.parseColor("#03A9F4"))
                .setNomalImg(R.drawable.msgfalse)
                .setSelImg(R.drawable.msgtrue)
                .setNomalsize(5)
                .setselSize(7)
                .setLinearlisenner(this)
                .refresh();
        mine.setContent("我的")
                .setnomalColor(Color.BLACK)
                .setselColor(Color.parseColor("#03A9F4"))
                .setNomalImg(R.drawable.user)
                .setSelImg(R.drawable.usertrue)
                .setNomalsize(5)
                .setselSize(7)
                .setLinearlisenner(this)
                .refresh();

        fragmentManager = getSupportFragmentManager();
        fragTransaction = fragmentManager.beginTransaction();
        if (goodsFragment == null){
            index.setSelFlag(true);
            goodsFragment = (Fragment) ARouter.getInstance()
                    .build("/com/bw/goods/mvvm/view/fragment/HomeGoodsFragment").navigation();
            fragTransaction.add(R.id.framelayout,goodsFragment);
        }else {
            fragTransaction.show(goodsFragment);
        }
        fragTransaction.commit();

        MyReciver myReciver = new MyReciver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("buycar");
        registerReceiver(myReciver,intentFilter);
    }

    @Override
    public void onSelect(int id) {
        clearBottomsel();
        hidefragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
        switch (id){
            case R.id.index:
                index.setSelFlag(true);
                if (goodsFragment == null){
                    goodsFragment = new HomeGoodsFragment();
                    goodsFragment = (Fragment) ARouter.getInstance()
                            .build("/com/bw/goods/mvvm/view/fragment/HomeGoodsFragment").navigation();
                    fragTransaction.add(R.id.framelayout,goodsFragment);
                }else {
                    fragTransaction.show(goodsFragment);
                }
                fragTransaction.commit();
                break;
            case R.id.sort:
                sort.setSelFlag(true);
                if (categoryFragment == null){
                    categoryFragment = new CategoryFragment();
                    categoryFragment = (Fragment) ARouter.getInstance()
                            .build("/com/bw/goods/mvvm/view/fragment/CategoryFragment").navigation();
                    fragTransaction.add(R.id.framelayout,categoryFragment);
                }else {
                    fragTransaction.show(categoryFragment);
                }
                fragTransaction.commit();
                break;
            case R.id.buyCar:
                buyCar.setSelFlag(true);
                if (carFragment == null){
                    carFragment = new CarFragment();
                    carFragment = (Fragment) ARouter.getInstance()
                            .build("/shopping/CarFragment").navigation();
                    fragTransaction.add(R.id.framelayout,carFragment);
                }else {
                    fragTransaction.show(carFragment);
                }
                fragTransaction.commit();

                break;
            case R.id.news:
                news.setSelFlag(true);
                if (messageFragment == null){
                    messageFragment = new MessageFragment();
                    messageFragment = (Fragment) ARouter.getInstance()
                            .build("/messagefragment/sbsc").navigation();
                    fragTransaction.add(R.id.framelayout,messageFragment);
                }else {
                    fragTransaction.show(messageFragment);
                }
                fragTransaction.commit();
                break;
            case R.id.mine:
                mine.setSelFlag(true);
                if (userFragment == null){
                    userFragment = new UserFragment();
                    userFragment = (Fragment) ARouter.getInstance()
                            .build("/intent/user/fragment").navigation();
                    fragTransaction.add(R.id.framelayout,userFragment);
                }else {
                    fragTransaction.show(userFragment);
                }
                fragTransaction.commit();
                break;
        }
    }
    private void clearBottomsel(){
        news.setSelFlag(false);
        index.setSelFlag(false);
        mine.setSelFlag(false);
        buyCar.setSelFlag(false);
        sort.setSelFlag(false);
    }
    private void hidefragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragTransaction = fragmentManager.beginTransaction();

        if (goodsFragment != null) {
            fragTransaction.hide(goodsFragment);
        }
        if (categoryFragment != null) {
            fragTransaction.hide(categoryFragment);
        }
        if (carFragment != null) {
            fragTransaction.hide(carFragment);
        }
        if (messageFragment != null) {
            fragTransaction.hide(messageFragment);
        }
        if (userFragment != null) {
            fragTransaction.hide(userFragment);
        }
        fragTransaction.commit();

    }
    class MyReciver extends BroadcastReceiver{

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("buycar")){
                    fragTransaction = fragmentManager.beginTransaction();
                    clearBottomsel();
                    if (goodsFragment != null) {
                        fragTransaction.hide(goodsFragment);
                    }
                    buyCar.setSelFlag(true);
                    if (carFragment == null){
                        carFragment = new CarFragment();
                        carFragment = (Fragment) ARouter.getInstance()
                                .build("/shopping/CarFragment").navigation();
                        fragTransaction.add(R.id.framelayout,carFragment);
                    }else {
                        fragTransaction.show(carFragment);
                    }
                    fragTransaction.commitAllowingStateLoss();
                }
            }
        }
}